function CommentWithReply(param) {

	var that = this;

	this.j_comment_add_form = null;
	this.j_comment_reply_form = null;
	this.j_comment_add_textarea = null;
	this.j_comment_reply_textarea = null;
	this.j_comment_entry_content = null;
	this.j_comment_entry_options = null;
	this.j_add_reply_link = null;
	this.j_add_comment_link = null;

	var delete_comment_url = "";
	
	var comment_adding = false;
	var comment_replying = false;

	function __construct(p) {
	try{
		for(pkey in p) {
			if(pkey[0] != "_") {
				that[pkey] = p[pkey];
			}

			if(p._delete_comment_url) {
				delete_comment_url = p._delete_comment_url;
			}
		}
		
		that.j_comment_add_textarea.live('focus', addCommentTextareaFocus).live('focusout', addCommentTextareaFocusout);
		that.j_comment_reply_textarea.live('focus', addReplyTextareaFocus).live('focusout', addReplyTextareaFocusout);
		that.j_comment_add_form.live('submit', addCommentFormSubmit);
		that.j_comment_reply_form.live('submit', addReplyFormSubmit);
		
		that.j_comment_entry_content.live('mouseenter', entryContentMouseenter).live('mouseleave', entryContentMouseleave);
		that.j_add_comment_link.live('click', addCommentLinkClick);
		that.j_add_reply_link.live('click', addReplyLinkClick);
		that.j_comment_entry_options.live('click', closeCommentEntryClick);
		
	}catch(err) {
		alert(err);
	}
	}
	
	function entryContentMouseenter(e) {
	try{
		jq(this).find("[data-comment-dom-id=entry-options]").show();
	}catch(err) {
		alert(err);
	}
	}
	
	function entryContentMouseleave(e) {
	try{
		jq(this).find("[data-comment-dom-id=entry-options]").hide();
	}catch(err) {
		alert(err);
	}
	}

	function addCommentTextareaFocus(e) {
	try{
		var add_comment_textarea = jq(this);
		add_comment_textarea.css({ "height" : "28px" });
		var form_container = add_comment_textarea.closest("[data-comment-dom-id=add-comment-form-container]");
		form_container.find("[data-comment-dom-id=avatar]").show();
		form_container.find("[data-comment-dom-id=add-comment-submit]").show();
		form_container.find("[data-comment-dom-id=add-comment-textarea-container]").css({ "margin" : "0 0 0 58px", "min-height" : "50px" });
	}catch(err) {
		alert(err);
	}
	}
	
	function addCommentTextareaFocusout(e) {
	try{
		var add_comment_textarea = jq(this);
		var val = jq.trim(add_comment_textarea.val());
		if(val.length == 0) {
			add_comment_textarea.val("").css({ "height" : "14px" });
			var form_container = add_comment_textarea.closest("[data-comment-dom-id=add-comment-form-container]");
			form_container.find("[data-comment-dom-id=avatar]").hide();
			form_container.find("[data-comment-dom-id=add-comment-submit]").hide();
			form_container.find("[data-comment-dom-id=add-comment-textarea-container]").css({ "margin" : "0", "min-height" : "0" });
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function addReplyTextareaFocus(e) {
	try{
		jq(this).closest("[data-comment-dom-id=add-reply-form-container]").show();
	}catch(err) {
		alert(err);
	}
	}
	
	function addReplyTextareaFocusout(e) {
	try{
		var add_reply_textarea = jq(this);
		var val = jq.trim(add_reply_textarea.val());
		if(val.length == 0) {
			add_reply_textarea.val("");
			add_reply_textarea.closest("[data-comment-dom-id=add-reply-form-container]").hide();
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function addReplyLinkClick(e) {
	try{
		e.preventDefault();
		var entry = jq(this).closest("[data-comment-is-parent=true]");
		entry.find("[data-comment-dom-id=add-reply-textarea]").focus();
	}catch(err) {
		alert(err);
	}	
	}
	
	function addCommentLinkClick(e) {
	try{
		document.title += 1;
		e.preventDefault();
		that.j_comment_add_textarea.focus();
		
	}catch(err) {
		alert(err);
	}	
	}

	function addCommentFormSubmit(e) {
	try {
    	e.preventDefault();
		if(!comment_adding) {
			comment_adding = true;
			var add_comment_form = jq(this);
			addComment(add_comment_form);
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function addComment(add_comment_form) {
	try{
		var url = add_comment_form.attr("action");
		var topic_id = add_comment_form.find('input:hidden[name="topicId"]').val();
		var content = add_comment_form.find("[data-comment-dom-id=add-comment-textarea]").val();
		jq.post(url, { "topic_id": topic_id, "content": encodeURIComponent(content) }, addCommentSuccess)
		.error(function() {
	    	// alert("Oops! An error occurred while processing your request.");
			var pos_top = add_comment_form.offset().top;
    		jq.notification({ "pos_top" : pos_top, "width" : 360, "message" : "Oops! An error occurred while processing your request." });
	    })
	    .complete(function() {
	   		// complete
			comment_adding = false;
	    });

	}catch(err) {
		alert(err);
	}
	}

	function addCommentSuccess(data) {
	try{
		var topic_id = data.topicId;
		var comment_entry_container = jq(document).find("[data-comment-topic-id=" + topic_id + "]");
		comment_entry_container.find("[data-comment-dom-id=add-comment-textarea]").val("").focusout();
		comment_entry_container.find("[data-comment-dom-id=entry-container]").prepend(data.html);
	}catch(err) {
		alert(err);
	}
	}
	
	function addReplyFormSubmit(e) {
	try {
    	e.preventDefault();
		if(!comment_replying) {
			comment_replying = true;
			var add_reply_form = jq(this);
			addReply(add_reply_form);
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function addReply(add_reply_form) {
	try{
		var content = add_reply_form.find("[data-comment-dom-id=add-reply-textarea]").val();
		var parent_entry = add_reply_form.closest("[data-comment-is-parent=true]");
		var url = add_reply_form.attr("action");
		var topic_id = add_reply_form.find('input:hidden[name="topicId"]').val();

		var parent_id = parent_entry.data("comment-id");
		
		jq.post(url, { "topic_id": topic_id, "parent_id": parent_id, "content": encodeURIComponent(content) }, addReplySuccess)
		.error(function() {
	    	// alert("Oops! An error occurred while processing your request.");
			var pos_top = add_reply_form.offset().top;
    		jq.notification({ "pos_top" : pos_top, "width" : 360, "message" : "Oops! An error occurred while processing your request." });
	    })
	    .complete(function() {
	   		// complete
	    	comment_replying = false;
	    });

	}catch(err) {
		alert(err);
	}
	}

	function addReplySuccess(data) {
	try{
		var topic_id = data.topicId;
		var comment_entry_container = jq(document).find("[data-comment-topic-id=" + topic_id + "]");
		var parent_id = data.parentId;
		var parent_entry = comment_entry_container.find("[data-comment-id=" + parent_id + "]");
		parent_entry.find("[data-comment-dom-id=add-reply-textarea]").val("").focusout();
		var child_entry_container = parent_entry.find("[data-comment-dom-id=child-entry-container]");
		child_entry_container.append(data.html);
		
	}catch(err) {
		alert(err);
	}
	}
	
	function closeCommentEntryClick(e) {
	try{
		e.preventDefault();
		var close_link = jq(this);
		var comment_entry = close_link.closest("[data-comment-id]");
		var comment_id = comment_entry.data("comment-id");
		var is_parent = comment_entry.data("comment-is-parent");
		deleteComment(close_link, comment_id, is_parent);
	}catch(err) {
		alert(err);
	}
	}
	
	function deleteComment(close_link, comment_id, is_parent) {
	try{
		jq.confirm({"message" : "Are you sure to delete this comment?"}, function(result) {
			if(result === true) {
				jq.get(delete_comment_url, { "comment_id": comment_id, "is_parent" : is_parent }, deleteCommentSuccess)
				.error(function() {
		    		// alert("Oops! An error occurred while processing your request.");
					var pos_top = close_link.offset().top;
		    		jq.notification({ "pos_top" : 200, "width" : 360, "message" : "Oops! An error occurred while processing your request." });
		    	})
		    	.complete(function() {
		   			// complete
		    	});
			}
		});
		
	}catch(err) {
		alert(err);
	}
	}

	function deleteCommentSuccess(data) {
	try{
		jq(document).find("[data-comment-id=" + data + "]").remove();
	}catch(err) {
		alert(err);
	}
	}
	
	__construct(param);
	
}

